// CommandLineInterface.h
#ifndef COMMAND_LINE_INTERFACE_H
#define COMMAND_LINE_INTERFACE_H

#include <string>
#include <iostream>
#include <memory>
#include <vector>
#include <map>
#include <stdexcept>

namespace ebackup {
namespace plugins {

class CommandLineInterface {
public:
    CommandLineInterface(const std::string& command);
    ~CommandLineInterface();

    void run();

private:
    std::string command_;
    std::map<std::string, std::function<void()>> commandMap_;

    void initializeCommands();
    void executeCommand(const std::string& command);
    void handleHelp();
    void handleDiscover();
    void handleBackup();
    void handleRecover();
};

} // namespace plugins
} // namespace ebackup

#endif // COMMAND_LINE_INTERFACE_H






// CommandLineInterface.cpp
#include "CommandLineInterface.h"
#include "RedhatVMDiscovery.h"
#include "RedhatVMBackup.h"
#include "RedhatVMRecovery.h"
#include "Logger.h"

namespace ebackup {
namespace plugins {

CommandLineInterface::CommandLineInterface(const std::string& command)
    : command_(command) {
    initializeCommands();
}

CommandLineInterface::~CommandLineInterface() {
    // Destructor
}

void CommandLineInterface::run() {
    if (commandMap_.find(command_) != commandMap_.end()) {
        commandMap_[command_]();
    } else {
        std::cerr << "Unknown command: " << command_ << std::endl;
        handleHelp();
    }
}

void CommandLineInterface::initializeCommands() {
    commandMap_["help"] = [this]() { handleHelp(); };
    commandMap_["discover"] = [this]() { handleDiscover(); };
    commandMap_["backup"] = [this]() { handleBackup(); };
    commandMap_["recover"] = [this]() { handleRecover(); };
}

void CommandLineInterface::executeCommand(const std::string& command) {
    if (commandMap_.find(command) != commandMap_.end()) {
        commandMap_[command]();
    } else {
        std::cerr << "Unknown command: " << command << std::endl;
        handleHelp();
    }
}

void CommandLineInterface::handleHelp() {
    std::cout << "Available commands:" << std::endl;
    std::cout << "  help - Display this help message" << std::endl;
    std::cout << "  discover - Discover Redhat virtual machines" << std::endl;
    std::cout << "  backup - Perform a backup of a virtual machine" << std::endl;
    std::cout << "  recover - Recover a virtual machine from a backup" << std::endl;
}

void CommandLineInterface::handleDiscover() {
    RedhatVMDiscovery discovery;
    std::vector<VirtualMachine> vms = discovery.discover();
    for (const auto& vm : vms) {
        std::cout << "VM: " << vm.name << ", CPUs: " << vm.cpus << ", Memory: " << vm.memory_mb << "MB, State: " << vm.state << std::endl;
    }
}

void CommandLineInterface::handleBackup() {
    RedhatVMBackup backup;
    BackupPolicy policy;
    policy.type = BackupPolicy::FULL;
    policy.schedule = "daily";
    policy.retention_days = 7;

    VirtualMachine vm;
    vm.name = "example_vm";
    vm.cpus = 2;
    vm.memory_mb = 4096;
    vm.state = "running";

    BackupResult result = backup.backup(vm, policy);
    if (result.success) {
        std::cout << "Backup completed successfully: " << result.message << std::endl;
    } else {
        std::cerr << "Backup failed: " << result.message << std::endl;
    }
}

void CommandLineInterface::handleRecover() {
    RedhatVMRecovery recovery;
    RecoveryPoint point;
    point.id = "backup_20231009";
    point.timestamp = "2023-10-09T12:00:00Z";
    point.type = RecoveryPoint::FULL;

    VirtualMachine vm;
    vm.name = "example_vm";
    vm.cpus = 2;
    vm.memory_mb = 4096;
    vm.state = "stopped";

    RecoveryResult result = recovery.recover(vm, point);
    if (result.success) {
        std::cout << "Recovery completed successfully: " << result.message <<